<template>
  <div class="maintenance-page">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>维修工单管理</span>
          <el-button type="primary" @click="openForm('add')">新建工单</el-button>
        </div>
      </template>

      <el-table :data="maintenanceList" style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="工单ID" width="100" />
        <el-table-column prop="userId" label="用户" width="160">
  <template #default="scope">
    {{ userMap[scope.row.userId] || scope.row.userId }}
  </template>
</el-table-column>
        <el-table-column prop="description" label="故障描述" />
        <el-table-column prop="reportTime" label="报修时间" width="180" />
        <el-table-column prop="status" label="状态" width="120">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ statusTextMap[scope.row.status] || '未知' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="repairUserId" label="维修人员" width="160">
  <template #default="scope">
    {{ userMap[scope.row.repairUserId] || scope.row.repairUserId }}
  </template>
</el-table-column>        <el-table-column prop="assignTime" label="分配时间" width="180" />
        <el-table-column label="操作" width="200">
          <template #default="scope">
            <el-button size="small" @click="openForm('edit', scope.row)">编辑</el-button>
            <el-button size="small" type="primary" @click="showDetail(scope.row)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog v-model="formVisible" :title="formMode === 'add' ? '新建工单' : '编辑工单'" width="600px">
      <el-form :model="formData" label-width="100px">
        <el-form-item label="用户">
  <el-select v-model="formData.userId" placeholder="请选择用户" filterable>
    <el-option
      v-for="user in userOptions"
      :key="user.id"
      :label=" user.userName"
      :value="user.id"
    />
  </el-select>
</el-form-item>

        <el-form-item label="故障描述">
          <el-input v-model="formData.description" type="textarea" />
        </el-form-item>
        <el-form-item label="报修时间">
          <el-date-picker v-model="formData.reportTime" type="datetime" />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formData.status" placeholder="请选择">
            <el-option v-for="(text, val) in statusTextMap" :key="val" :label="text" :value="parseInt(val)" />
          </el-select>
        </el-form-item>
        
<el-form-item label="维修人员">
  <el-select v-model="formData.repairUserId" placeholder="请选择维修人员" filterable>
    <el-option
      v-for="user in userOptions"
      :key="user.id"
      :label="user.userName"
      :value="user.id"
    />
  </el-select>
</el-form-item>
        <el-form-item label="分配时间">
          <el-date-picker v-model="formData.assignTime" type="datetime" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="formVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </template>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog v-model="detailVisible" title="工单详情" width="600px">
      <el-descriptions :column="1" border>
        <el-descriptions-item label="故障描述">{{ detailData.description }}</el-descriptions-item>
        <el-descriptions-item label="报修时间">{{ detailData.reportTime }}</el-descriptions-item>
        <el-descriptions-item label="状态">{{ statusTextMap[detailData.status] }}</el-descriptions-item>
        <el-descriptions-item label="分配时间">{{ detailData.assignTime }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <el-button @click="detailVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import {
  listRepairorder,
  getRepairorder,
  addRepairorder,
  updateRepairorder
} from '@/api/repairorder'
import { listUser } from '@/api/user' // 假设你把 listUser 放在 @/api/user.js

const userOptions = ref([])
const userMap = ref({}) // userId => username

const fetchUsers = async () => {
  const res = await listUser({})
  userOptions.value = res || []
  userMap.value = {}
  userOptions.value.forEach(user => {
    userMap.value[user.id] = user.userName 
  })
}

const maintenanceList = ref([])
const loading = ref(false)

const statusTextMap = {
  0: '待处理',
  1: '维修中',
  2: '维修完成',
  3: '已归档'
}

const getStatusType = (status) => {
  const typeMap = {
    0: 'warning',
    1: 'primary',
    2: 'success',
    3: 'info'
  }
  return typeMap[status] || 'info'
}

// 初始化数据
const fetchData = async () => {
  loading.value = true
  try {
    const res = await listRepairorder({})
    maintenanceList.value = res 
  } finally {
    loading.value = false
  }
}
onMounted(() => {
  fetchUsers()
  fetchData()
})

// ========== 表单相关 ==========
const formVisible = ref(false)
const formMode = ref('add') // 'add' or 'edit'
const formData = reactive({
  id: null,
  userId: '',
  description: '',
  reportTime: '',
  status: 0,
  repairUserId: '',
  assignTime: ''
})

const openForm = async (mode, row = {}) => {
  formMode.value = mode
  if (mode === 'edit' && row.id) {
    const res = await getRepairorder(row.id)
    Object.assign(formData, res)
  } else {
    Object.assign(formData, {
      id: null,
      userId: '',
      description: '',
      reportTime: '',
      status: 0,
      repairUserId: '',
      assignTime: ''
    })
  }
  formVisible.value = true
}

const submitForm = async () => {
  if (formMode.value === 'add') {
    await addRepairorder(formData)
  } else {
    await updateRepairorder(formData)
  }
  formVisible.value = false
  fetchData()
}

// ========== 详情相关 ==========
const detailVisible = ref(false)
const detailData = reactive({
  id: '',
  userId: '',
  description: '',
  reportTime: '',
  status: '',
  repairUserId: '',
  assignTime: ''
})

const showDetail = async (row) => {
  const res = await getRepairorder(row.id)
  Object.assign(detailData, res)
  detailVisible.value = true
}
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
